package com.lovo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.dao.MasterMapper;
import com.lovo.model.Master;
import com.lovo.service.MasterService;
import com.lovo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MasterServiceImpl implements MasterService {
    @Override
    public List<Master> getAll(Master record) {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        MasterMapper mapper=sqlSession.getMapper(MasterMapper.class);
        List<Master> list=mapper.getAll(record);
        SqlSessionUtil.commit();
        return list;
    }

    @Override
    public boolean insert(Master record) {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        MasterMapper mapper=sqlSession.getMapper(MasterMapper.class);
        int num=mapper.insert(record);
        SqlSessionUtil.commit();
        return num>-1?true:false;
    }

    @Override
    public boolean update(Master record) {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        MasterMapper mapper=sqlSession.getMapper(MasterMapper.class);
        int num=mapper.update(record);
        SqlSessionUtil.commit();
        return  num>-1?true:false;
    }

    @Override
    public boolean delete(Master record) {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        MasterMapper mapper=sqlSession.getMapper(MasterMapper.class);
        int num=mapper.delete(record);
        SqlSessionUtil.commit();
        return  num>-1?true:false;
    }

    @Override
    public Master show(Master master) {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        MasterMapper mapper=sqlSession.getMapper(MasterMapper.class);
        Master m=mapper.show(master);
        SqlSessionUtil.commit();
        return  m;
    }

    @Override
    public int getMasterId(Master master) {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        MasterMapper mapper=sqlSession.getMapper(MasterMapper.class);
        int m=mapper.getMasterId(master);
        SqlSessionUtil.commit();
        return  m;
    }

    @Override
    public PageInfo<Master> getPageAll(Master master, int pageNo, int pageSize) {

        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        PageHelper.startPage(pageNo,pageSize);
        MasterMapper mapper=sqlSession.getMapper(MasterMapper.class);
        List<Master> list=mapper.getAll(master);
       PageInfo<Master> pageInfo=new PageInfo<>(list);
         SqlSessionUtil.commit();
        return pageInfo;
    }
}
